ChannelService    = Packages.com.google.appengine.api.channel.ChannelService;
ChannelServiceFactory = Packages.com.google.appengine.api.channel.ChannelServiceFactory;
ChannelMessage = Packages.com.google.appengine.api.channel.ChannelMessage;

function main( ui, hh ) {

  var json = ui.getQueryParameters().getFirst("json");
  
  var jsonObject = new JSONObject(json);
  var transactions = getTransactions (jsonObject);
  var message = "{'exception' : {'cause':1,'message':'The transaction already exist, contact the admin'}}";
  if ( transactions.length() == 0 )
       return createTransaction( jsonObject );
  
  if ( transactions.length() == 1 ){
       var stored = transactions.get(0);
  
  if ( stored.get('subType').equals(jsonObject.get('subType'))){
       return message;
  }
  else 
       return createTransaction( jsonObject );
  }
  
  if ( transactions.length() == 2 )
       return message;  
}


function getTransactions(jsonObject) {

  var records = new JSONArray();

  var cmd = "select t from TransactionEntity t where t.refId=:refId";
  
  
    EntityManager.find(  
    cmd,  
    jsonObject,
     function(result) {
      
      for( var i = 0; i < result.size() ; ++i ) {
        
        records.put( JSONObject.wrap(result.get(i)) );
      }
      
    });
    
    var result = new JSONObject();
    
    result.put( "records", records );
    

    return records ;

}

function createTransaction( o ) {
 
  var t = new TransactionEntity();
  var exception = 
  t.userId=o.getString("userId");
  t.userAgent=o.getString("userAgent");
  t.longDate = o.getLong("longDate");
  t.type = o.getString("type");
  t.subType = o.getString("subType");
  t.refId = o.getString("refId");
  t.lat = o.getDouble("lat");
  t.lng = o.getDouble("lng");
  t.attributes = o.getString("attributes");
  //t.attributes = toJSON( { lat: o.getDouble("lat"), lng: o.getDouble("lng") } );
  
  try{
      EntityManager.createTransaction( t );
  } catch( e ){
       
        return "{'exception':{'code':3,'message':'Error creating transaction','cause':" + e.getMessage() + "}}"
  }
  
  sendMessage( toJSON( t ) );
  return toJSON( t );
  
  

}

function sendMessage(trans){

  try{
     var msg = new ChannelMessage( "badge" , trans);
     var channelService = ChannelServiceFactory.getChannelService();
     channelService.sendMessage(msg); 
  } catch(e){
      Log.debug("the channel is not open" + e);
  }
  

 return; 
}

/*
function main( ui, hh ) {

  var json = ui.getQueryParameters().getFirst("json");
  
  //return json;
  return createTransaction( json );
}



function createTransaction( json ) {
 
  var o = new JSONObject( json );
  
  var t = new TransactionEntity();
  
  t.userId=o.getString("userId");
  t.userAgent=o.getString("userAgent");
  t.longDate = o.getLong("longDate");
  t.type = o.getString("type");
  t.subType = o.getString("subType");
  t.refId = o.getString("refId");
  t.lat = o.getDouble("lat");
  t.lng = o.getDouble("lng");
  t.attributes = '';
  //t.attributes = toJSON( { lat: o.getDouble("lat"), lng: o.getDouble("lng") } );
  
  
  EntityManager.createTransaction( t );
  
  return toJSON( t );

}

*/